package com.funsport.controller.web;

import com.funsport.common.Result;
import com.funsport.service.IStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

/**
 * Web管理端数据统计Controller
 *
 * @author Fun-Sport Team
 */
@Tag(name = "Web管理端数据统计", description = "Web管理端数据统计接口")
@RestController
@RequestMapping("/web/statistics")
@RequiredArgsConstructor
public class WebStatisticsController {

    private final IStatisticsService statisticsService;

    @Operation(summary = "获取平台总体统计")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/platform")
    public Result<Map<String, Object>> getPlatformStatistics() {
        Map<String, Object> stats = statisticsService.getPlatformStatistics();
        return Result.success(stats);
    }

    @Operation(summary = "获取用户统计")
    @PreAuthorize("hasAnyRole('ADMIN', 'VENUE_MANAGER')")
    @GetMapping("/users")
    public Result<Map<String, Object>> getUserStatistics(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "30") Integer days) {
        // 如果没有提供日期范围，则根据days计算
        if (startDate == null || endDate == null) {
            endDate = LocalDate.now();
            startDate = endDate.minusDays(days);
        }
        Map<String, Object> stats = statisticsService.getUserStatistics(startDate, endDate);
        return Result.success(stats);
    }

    @Operation(summary = "获取俱乐部统计")
    @PreAuthorize("hasAnyRole('ADMIN', 'CLUB_ADMIN')")
    @GetMapping("/clubs")
    public Result<Map<String, Object>> getClubStatistics(
            @RequestParam(required = false) Long clubId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "30") Integer days) {
        // 如果没有提供日期范围，则根据days计算
        if (startDate == null || endDate == null) {
            endDate = LocalDate.now();
            startDate = endDate.minusDays(days);
        }
        Map<String, Object> stats = statisticsService.getClubStatistics(clubId, startDate, endDate);
        return Result.success(stats);
    }

    @Operation(summary = "获取活动统计")
    @PreAuthorize("hasAnyRole('ADMIN', 'VENUE_MANAGER')")
    @GetMapping("/events")
    public Result<Map<String, Object>> getEventStatistics(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "30") Integer days) {
        // 如果没有提供日期范围，则根据days计算
        if (startDate == null || endDate == null) {
            endDate = LocalDate.now();
            startDate = endDate.minusDays(days);
        }
        Map<String, Object> stats = statisticsService.getEventStatistics(startDate, endDate);
        return Result.success(stats);
    }

    @Operation(summary = "获取场地统计")
    @PreAuthorize("hasAnyRole('ADMIN', 'VENUE_MANAGER')")
    @GetMapping("/venues")
    public Result<Map<String, Object>> getVenueStatistics(
            @RequestParam(required = false) Long venueId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(defaultValue = "30") Integer days) {
        // 如果没有提供日期范围，则根据days计算
        if (startDate == null || endDate == null) {
            endDate = LocalDate.now();
            startDate = endDate.minusDays(days);
        }
        Map<String, Object> stats = statisticsService.getVenueStatistics(venueId, startDate, endDate);
        return Result.success(stats);
    }

    @Operation(summary = "获取用户活跃度统计")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/user-activity")
    public Result<Map<String, Object>> getUserActivityStatistics(
            @RequestParam(defaultValue = "30") Integer days) {
        Map<String, Object> stats = statisticsService.getUserActivityStatistics(days);
        return Result.success(stats);
    }

    @Operation(summary = "获取运动类型热度统计")
    @PreAuthorize("hasAnyRole('ADMIN', 'VENUE_MANAGER')")
    @GetMapping("/sport-popularity")
    public Result<Map<String, Object>> getSportTypePopularity(
            @RequestParam(defaultValue = "30") Integer days) {
        Map<String, Object> stats = statisticsService.getSportTypePopularity(days);
        return Result.success(stats);
    }

    @Operation(summary = "获取城市活动分布统计")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/city-distribution")
    public Result<Map<String, Object>> getCityDistribution(
            @RequestParam(defaultValue = "30") Integer days) {
        Map<String, Object> stats = statisticsService.getCityDistribution(days);
        return Result.success(stats);
    }

    @Operation(summary = "获取俱乐部排行榜")
    @PreAuthorize("hasAnyRole('ADMIN', 'VENUE_MANAGER')")
    @GetMapping("/club-rankings")
    public Result<Map<String, Object>> getClubRankings(
            @RequestParam(defaultValue = "activity") String type,
            @RequestParam(defaultValue = "10") Integer limit) {
        Map<String, Object> rankings = statisticsService.getClubRankings(type, limit);
        return Result.success(rankings);
    }

    @Operation(summary = "获取用户排行榜")
    @PreAuthorize("hasAnyRole('ADMIN', 'VENUE_MANAGER')")
    @GetMapping("/user-rankings")
    public Result<Map<String, Object>> getUserRankings(
            @RequestParam(defaultValue = "points") String type,
            @RequestParam(defaultValue = "10") Integer limit) {
        Map<String, Object> rankings = statisticsService.getUserRankings(type, limit);
        return Result.success(rankings);
    }
}

